package com.service.impl;

import com.dao.TeacherDao;
import com.domain.*;
import com.service.TeacherService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;
    private int pageSize = 6;

    /**
     * 根据课程号查询所有学生的信息
     * @param courseId
     * @return
     */
    @Override
    public PageBean findStuByCid(int currentPage,int courseId) {
        int total = teacherDao.totalStu(courseId);
        int totalPage = 0;
        if (total % pageSize == 0) {
            totalPage = total / pageSize;
        }else {
            totalPage = total / pageSize + 1;
        }
        int start = (currentPage - 1) * pageSize;
        List<StudentGrade> list = teacherDao.findStuByCid(courseId,start,pageSize);
        PageBean bean = new PageBean(pageSize,currentPage,totalPage,start,list);
        return bean;
    }
    /**
     * 打分
     * @param course
     */
    @Override
    public void markStu(SelectCourse course) {
        teacherDao.markStu(course);
    }
    /**
     * 查询教师授课的总数
     * @param teacherID
     * @return
     */
    @Override
    public int findTotal(int teacherID) {
        return teacherDao.findTotal(teacherID);
    }
    /**
     * 查询所有教师信息
     * @return
     */
    @Override
    public List<Teacher> findAllTeacher() {
        return teacherDao.findAllTeacher();
    }
    /**
     * 查询所有院系信息
     * @return
     */
    @Override
    public List<College> findAllCollege() {
        return teacherDao.findAllCollege();
    }

    /**
     * 查询教师数量
     * @param name
     * @return
     */
    public int totalTeacher(String name){
        if (name == null) {
            //查询全部
            return teacherDao.totalTeacher(null);
        }else {
            //模糊查询
            return teacherDao.totalTeacher("%" + name + "%");
        }
    }
    /**
     * 查询教师信息并进行分页处理
     * @param name
     * @param currentPage
     * @return
     */
    @Override
    public PageBean findAndSearchTeacher(String name, int currentPage) {
        int total = 0;
        if (name == null) {
            //当name为null时，表示查询全部
            total = teacherDao.totalTeacher(null);
        }else {
            //当name有值时，表示模糊查询
            total = teacherDao.totalTeacher("%" + name + "%");
        }
        int totalPage = 0;
        if (total % pageSize == 0) {
            totalPage = total / pageSize;
        }else {
            totalPage = total / pageSize + 1;
        }
        int start = (currentPage - 1) * pageSize;
        List<Teacher> list = null;
        if (name == null) {
            //查询全部
            list = teacherDao.findAndSearchTeacher(null,start,pageSize);
        }else {
            //模糊查询
            list = teacherDao.findAndSearchTeacher("%" + name + "%",start,pageSize);
        }
        PageBean bean = new PageBean(pageSize,currentPage,totalPage,start,list);
        return bean;
    }
    /**
     * 添加教师信息
     * @param teacher
     */
    @Override
    public void addTeacher(Teacher teacher) {
        teacherDao.addTeacher(teacher);
    }
    /**
     * 根据教师id查询教师信息
     * @param teacherID
     * @return
     */
    @Override
    public Teacher findTeacherById(int teacherID) {
        return teacherDao.findTeacherById(teacherID);
    }
    /**
     * 更新教师信息
     * @param teacher
     */
    @Override
    public void updateTeacher(Teacher teacher) {
        teacherDao.updateTeacher(teacher);
    }

    /**
     * 删除教师信息
     * @param id
     */
    @Override
    public void deleteTeacherById(int id) {
        teacherDao.deleteTeacherById(id);
    }
}
